package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqCompostProduceElectricityMapper;
import com.guofen.pojo.SjqCompostProduceElectricity;
import com.guofen.service.sjq.SjqCompostProduceElectricityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SjqCompostProduceElectricityServiceImpl implements SjqCompostProduceElectricityService {
    @Autowired
    SjqCompostProduceElectricityMapper sjqCompostProduceElectricityMapper;

    @Override
    public boolean add(String reading,
                       String reader,
                       String remarks){
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = dateFormat.format(now);
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String eReadingTime = timeFormat.format(now);

        SjqCompostProduceElectricity sjqCompostProduceElectricity = new SjqCompostProduceElectricity();
        sjqCompostProduceElectricity.setDate(date);
        sjqCompostProduceElectricity.setReadingTime(eReadingTime);
        sjqCompostProduceElectricity.setReading(Double.parseDouble(reading));
        sjqCompostProduceElectricity.setReader(reader);
        sjqCompostProduceElectricity.setRemarks(remarks);

        int result = sjqCompostProduceElectricityMapper.insert(sjqCompostProduceElectricity);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public SjqCompostProduceElectricity getlast_day(){
        return sjqCompostProduceElectricityMapper.getlast();
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqCompostProduceElectricity> list = sjqCompostProduceElectricityMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqCompostProduceElectricity> list = sjqCompostProduceElectricityMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqCompostProduceElectricityMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqCompostProduceElectricity record) {
        return sjqCompostProduceElectricityMapper.updateByPrimaryKeySelective(record);
    }

}
